/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ebdesk.model.masterdata;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.swing.JOptionPane;

/**
 *
 * @author Zulfikar
 */
public class Setting {
    
    //------- database --------
    private String connectionstring = "jdbc:postgresql://localhost/sppt";
    private String username = "postgres";
    private String password = "postgresql";
    private String driver = "org.postgresql.Driver";
    
    //--------- user management ------
    private int[] rowsPerPageUser = new int[]{5, 10, 15, 20, 50};
    
    //--------- printer management -----
    private int[] rowsPerPagePrinter = new int[]{5, 10, 15, 20, 50};
    
    //--------- report -------------------
    private String kode_kota = "123";
    private String kode_provinsi = "1234";
    private String nama_pejabat = "foke";
    private String nip_pejabat = "foked123";
    private String paging = "100";
    private String tanggal_terbit = "11-04-2010";
    private String tanggal_penetapan = "18-05-1989";
    private String tempat_pembayaran = "jakarta";
    
    public Setting() {
        load();
    }
    
    public Connection dbCon(){
        Connection c = null;
        
        try{
            Properties p = new Properties();
            FileInputStream in = new FileInputStream("config.properties");
            p.load(in);

            Class.forName(p.getProperty("driver"));   
            c = DriverManager.getConnection(p.getProperty("connectionstring"),p.getProperty("username"),p.getProperty("password"));
        }
        catch(FileNotFoundException e){
            System.err.println(e);
        }
        catch(IOException e){
            System.err.println(e);
        }
        catch(ClassNotFoundException e){
            System.err.println(e);
        }
        catch(SQLException e){
            System.err.println(e);
        }        
        finally{
            return c;
        }
    }
    
    public void update(String field, String value){
        Connection c = dbCon();
        Statement s;
        StringBuilder sb = new StringBuilder();
        try{
            s = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            sb.append("update pbb.pbb_setting set value = '").append(value).append("' where setting = '").append(field).append("'");            
            s.executeUpdate(sb.toString());         
        }
        catch(SQLException ex){
            ex.printStackTrace();
        }
        
    }
    
    public void store() {                
        Properties p = new Properties();
        
    	try {
            update("rows_per_page_user", processArrayToString(this.getRowsPerPageUser()));
            update("rows_per_page_printer", processArrayToString(this.getRowsPerPagePrinter()));
            update("kode_kota", this.getKode_kota());
            update("kode_provinsi", this.getKode_provinsi());
            update("nama_pejabat", this.getNama_pejabat());
            update("nip_pejabat", this.getNip_pejabat());
            update("paging", this.getPaging());
            update("tanggal_terbit",this.getTanggal_terbit());
            update("tanggal_penetapan", this.getTanggal_penetapan());
            update("tempat_pembayaran", this.getTempat_pembayaran());           
            
            //set the properties value
            p.setProperty("connectionstring", this.getConnectionstring());
            p.setProperty("username", this.getUsername());
            p.setProperty("password", this.getPassword());
            p.setProperty("driver", this.getDriver());
            //save properties to project root folder
            p.store(new FileOutputStream("config.properties"), null);
    	}
        catch (IOException ex) {
            ex.printStackTrace();
        }        
    }
    
    public String processArrayToString(int[] array) {
        String res = "";
        
        for (int i = 0; i < array.length - 1; i++) {
            res += array[i] + ",";
        }
        
        res += array[array.length - 1];
        
        return res;
    }
    
    public int[] processStringToArray(String str) {
        String[] ss = str.split(",");
        int[] res = new int[ss.length];
        
        for (int i = 0; i < res.length; i++) {
            res[i] = Integer.parseInt(ss[i].trim());
        }
        
        return res;
    }
    
    public void load() {
        Connection c = dbCon();
        Statement s;        
        ResultSet rs;
        Properties p = new Properties();
        
    	try {
            //load a properties file
            p.load(new FileInputStream("config.properties"));                

            //load settings and save to hashmap
            s = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = s.executeQuery("select * from pbb.pbb_setting");    

            Map<String, String> settings = new HashMap<String, String>();
            while(rs.next()){
                settings.put(rs.getString(1), rs.getString(2));
            }

            //get the property value and print it out
            this.setConnectionstring(p.getProperty("connectionstring"));                
            this.setUsername(p.getProperty("username"));
            this.setPassword(p.getProperty("password"));
            this.setDriver(p.getProperty("driver", this.getDriver()));
            this.setRowsPerPageUser(processStringToArray(settings.get("rows_per_page_user")));
            this.setRowsPerPagePrinter(processStringToArray(settings.get("rows_per_page_printer")));

            this.setKode_kota(settings.get("kode_kota"));
            this.setKode_provinsi(settings.get("kode_provinsi"));
            this.setNama_pejabat(settings.get("nama_pejabat"));
            this.setNip_pejabat(settings.get("nip_pejabat"));
            this.setPaging(settings.get("paging"));
            this.setTanggal_terbit(settings.get("tanggal_terbit"));
            this.setTanggal_penetapan(settings.get("tanggal_penetapan"));
            this.setTempat_pembayaran(settings.get("tempat_pembayaran"));                
    	}
        catch (IOException ex) {
            ex.printStackTrace();
        }
        catch (SQLException ex) {            
            ex.printStackTrace();
        }        
        catch (NullPointerException ex){
            throw new NullPointerException();
        }
    }
    
    public static void main(String[] args) {
        Setting setting = new Setting(); 
        
//        setting.connectionstring = "jdbc:postgresql://localhost/pbb";
//        setting.username = "postgres";
//        setting.password = "postgresql";
//        setting.driver = "org.postgresql.Driver";
//        setting.rowsPerPageUser = new int[]{5, 10, 15};        
        setting.store();
    }

    /**
     * @return the connectionstring
     */
    public String getConnectionstring() {
        return connectionstring;
    }

    /**
     * @param connectionstring the connectionstring to set
     */
    public void setConnectionstring(String connectionstring) {
        this.connectionstring = connectionstring;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the driver
     */
    public String getDriver() {
        return driver;
    }

    /**
     * @param driver the driver to set
     */
    public void setDriver(String driver) {
        this.driver = driver;
    }

    /**
     * @return the rowsPerPageUser
     */
    public int[] getRowsPerPageUser() {
        return rowsPerPageUser;
    }

    /**
     * @param rowsPerPageUser the rowsPerPageUser to set
     */
    public void setRowsPerPageUser(int[] rowsPerPageUser) {
        this.rowsPerPageUser = rowsPerPageUser;
    }

    /**
     * @return the rowsPerPagePrinter
     */
    public int[] getRowsPerPagePrinter() {
        return rowsPerPagePrinter;
    }

    /**
     * @param rowsPerPagePrinter the rowsPerPagePrinter to set
     */
    public void setRowsPerPagePrinter(int[] rowsPerPagePrinter) {
        this.rowsPerPagePrinter = rowsPerPagePrinter;
    }

    /**
     * @return the kode_kota
     */
    public String getKode_kota() {
        return kode_kota;
    }

    /**
     * @param kode_kota the kode_kota to set
     */
    public void setKode_kota(String kode_kota) {
        this.kode_kota = kode_kota;
    }

    /**
     * @return the kode_provinsi
     */
    public String getKode_provinsi() {
        return kode_provinsi;
    }

    /**
     * @param kode_provinsi the kode_provinsi to set
     */
    public void setKode_provinsi(String kode_provinsi) {
        this.kode_provinsi = kode_provinsi;
    }

    /**
     * @return the nama_pejabat
     */
    public String getNama_pejabat() {
        return nama_pejabat;
    }

    /**
     * @param nama_pejabat the nama_pejabat to set
     */
    public void setNama_pejabat(String nama_pejabat) {
        this.nama_pejabat = nama_pejabat;
    }

    /**
     * @return the nip_pejabat
     */
    public String getNip_pejabat() {
        return nip_pejabat;
    }

    /**
     * @param nip_pejabat the nip_pejabat to set
     */
    public void setNip_pejabat(String nip_pejabat) {
        this.nip_pejabat = nip_pejabat;
    }

    /**
     * @return the paging
     */
    public String getPaging() {
        return paging;
    }

    /**
     * @param paging the paging to set
     */
    public void setPaging(String paging) {
        this.paging = paging;
    }

    /**
     * @return the tanggal_penetapan
     */
    public String getTanggal_penetapan() {
        return tanggal_penetapan;
    }

    /**
     * @param tanggal_penetapan the tanggal_penetapan to set
     */
    public void setTanggal_penetapan(String tanggal_penetapan) {
        this.tanggal_penetapan = tanggal_penetapan;
    }

    /**
     * @return the tempat_pembayaran
     */
    public String getTempat_pembayaran() {
        return tempat_pembayaran;
    }

    /**
     * @param tempat_pembayaran the tempat_pembayaran to set
     */
    public void setTempat_pembayaran(String tempat_pembayaran) {
        this.tempat_pembayaran = tempat_pembayaran;
    }

    /**
     * @return the tanggal_terbit
     */
    public String getTanggal_terbit() {
        return tanggal_terbit;
    }

    /**
     * @param tanggal_terbit the tanggal_terbit to set
     */
    public void setTanggal_terbit(String tanggal_terbit) {
        this.tanggal_terbit = tanggal_terbit;
    }


    
}
